package org.eece262.taskshareserver;

import java.util.LinkedList;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.edu.eece262.taskshare.Messages;

@PersistenceCapable
public class Group {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

    @Persistent
    private String groupName;
    
    @Persistent
    private String groupID;  
    
    @Persistent
    private String groupOwner;
    
    @Persistent
    private boolean groupDeleted;
    
    @Persistent
    private LinkedList<String> groupMemberName;
	
    @Persistent
    private LinkedList<Integer> groupMemberRoll;
	
    @Persistent
    private LinkedList<Integer> groupMemberStatus;
    
    public Group() {
    	groupMemberName = new LinkedList<String>();
    	groupMemberRoll = new LinkedList<Integer>();
    	groupMemberStatus = new LinkedList<Integer>();
    	groupDeleted = false;
    }
    
	public Key getKey() {
        return key;
    }

	public String getGroupName() {
		return groupName;
	}

	public Group setGroupName(String groupName) {
		this.groupName = groupName;
		return this;
	}

	public String getGroupID() {
		return groupID;
	}

	public Group setGroupID(String groupID) {
		this.groupID = groupID;
		return this;
	}

	public String getGroupOwner() {
		return groupOwner;
	}

	public Group setGroupOwner(String groupOwner) {
		this.groupOwner = groupOwner;
		return this;
	}

	public LinkedList<GroupMembership> getGroupMembers() {
		LinkedList<GroupMembership> groupMembers = new LinkedList<GroupMembership>();
		for (int i = 0; i < groupMemberName.size(); i++) {
			groupMembers.add(new GroupMembership()
			.setGroupMemberUser(groupMemberName.get(i))
			.setGroupMemberRoll(groupMemberRoll.get(i))
			.setGroupMemberStatus(groupMemberStatus.get(i)));
		}
		return groupMembers;
	}

	public Group setGroupMembers(LinkedList<GroupMembership> groupMembers) {
		groupMemberName = new LinkedList<String>();
		groupMemberRoll = new LinkedList<Integer>();
		groupMemberStatus = new LinkedList<Integer>();
		for(int i = 0; i < groupMembers.size(); i++) {
			groupMemberName.add(groupMembers.get(i).getGroupMemberUser());
			groupMemberRoll.add(groupMembers.get(i).getGroupMemberRoll());
			groupMemberStatus.add(groupMembers.get(i).getGroupMemberStatus());
		}
		return this;
	}
	
	public Group addGroupMember(GroupMembership groupMember) {
		groupMemberName.add(groupMember.getGroupMemberUser());
		groupMemberRoll.add(groupMember.getGroupMemberRoll());
		groupMemberStatus.add(groupMember.getGroupMemberStatus());
		return this;
	}

	public Messages.Group getMessagesGroup() {
		Messages.Group group = new Messages.Group();
		group.setOwner(groupOwner);
		group.setGroupName(groupName);
		for(int i = 0; i < groupMemberName.size(); i++) {
			group.addMember(new Messages.GroupMember(groupMemberName.get(i), 
					groupMemberStatus.get(i), groupMemberRoll.get(i)));
		}
		return group;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public Group setGroupDeleted(boolean groupDeleted) {
		this.groupDeleted = groupDeleted;
		return this;
	}

	public boolean isGroupDeleted() {
		return groupDeleted;
	}

	public LinkedList<String> getGroupMemberName() {
		return groupMemberName;
	}

	public void setGroupMemberName(LinkedList<String> groupMemberName) {
		this.groupMemberName = groupMemberName;
	}

	public LinkedList<Integer> getGroupMemberRoll() {
		return groupMemberRoll;
	}

	public void setGroupMemberRoll(LinkedList<Integer> groupMemberRoll) {
		this.groupMemberRoll = groupMemberRoll;
	}

	public LinkedList<Integer> getGroupMemberStatus() {
		return groupMemberStatus;
	}

	public void setGroupMemberStatus(LinkedList<Integer> groupMemberStatus) {
		this.groupMemberStatus = groupMemberStatus;
	}
}